Page({
  data: {
    coachs: [],
    courseId:0,
    coursePrice:0,
    orders:{}
  },
  onLoad(options){
    this.data.courseId=options.courseId
    this.data.coursePrice=options.coursePrice
    this.coachsList()
  },
  coachsList: function() {
    let that = this;
    wx.request({
      url: 'http://localhost:8080/user/coach/findCoachforCourseid',
      method:'POST',
      header: {
        "Authorization" : wx.getStorageSync('token')
      },
      data:this.data.courseId,
      success(res){
        if(res.statusCode==200){
          let dat = res.data.data;
          that.setData({
            coachs:dat
          });
        }else{
          console.log("加载失败");
        }
      },
    });
  },
  shop: function(e) {
    let that = this;
    that.data.orders.orders1=that.data.courseId;//课程表
    that.data.orders.coachId=e.currentTarget.dataset.aa;//教练表
    wx.request({
      url: 'http://localhost:8080/user/user/pay',
      method:'POST',
      header: {
        "Authorization" : wx.getStorageSync('token')
      },
      data:that.data.coursePrice,
      success(res){
        if(res.statusCode==200){
          let dat = res.data.data;
          if(res.data.msg!=null){
            wx.showToast({
              title: res.data.msg,  // 必填
              icon: 'none', // 支持 'success'/'none'/'loading'
              duration: 1000   // 自动关闭时间（毫秒）
            })
          }else{
            wx.request({
              url: 'http://localhost:8080/orders/orders/addOrders',
              method:'POST',
              header: {
                "Authorization" : wx.getStorageSync('token')
              },
              data:that.data.orders,
              success(res){
                if(res.statusCode==200){
                  let dat = res.data.data;
                  wx.switchTab({
                    url: '/pages/home/home'
                  })
                  wx.showToast({
                    title: "购买成功",  // 必填
                  })
                }else{
                  console.log("加载失败");
                }
              },
            });
            
          }
        }else{
          console.log("加载失败");
        }
      },
    });
  }
})